import axios from 'axios'
import message from '../plugins/message'

const service = axios.create({
    baseURL: 'http://api.star.ms100.com.cn',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
// 添加请求拦截器
service.interceptors.request.use(
    function (config) {
        let userinfo = localStorage.getItem('userinfo')
        if (userinfo) {
            config.headers.token = JSON.parse(userinfo).token
        }

        if (config.data && !(config.data instanceof FormData)) {
            config.data = new URLSearchParams(config.data)
        }
        return config;
    },
    function (error) {
        return Promise.reject(error);
    }
);
// 添加响应拦截器
service.interceptors.response.use(
    function (response) {
        if (response.data.code == 0) {
            message.$message({
                content: response.data.msg
            })
        }
        return response.data;
    },
    function (error) {
        message.$message({
            content: '网络错误，请稍后重试'
        })
        return Promise.reject(error);
    }
)
export default service